/**
 * @Author: wanqian
 * @Date: 2021/1/29 17:03
 */
public class Maze {

    public static void main(String[] args) {
        int[][] map = createMap();

        printMap(map);

        findWay(map, 1, 1);
        System.out.println("寻路完成");
        printMap(map);

    }

    /**
     * 寻路
     * @param map
     * @param i
     * @param j
     */
    public static boolean findWay(int[][] map, int i, int j) {
        //若找到了则终止寻路
        if (map[6][1] == 2) {
            System.out.println("已找到终点");
            return true;
        } else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                if (findWay(map, i + 1, j)) {
                    System.out.println(String.format("map[%d][%d]可以走通", i + 1, j));
                    return true;
                } else if (findWay(map, i, j + 1)) {
                    System.out.println(String.format("map[%d][%d]可以走通", i, j+1));
                    return true;
                } else if (findWay(map, i - 1, j)) {
                    System.out.println(String.format("map[%d][%d]可以走通", i-1, j));
                    return true;
                } else if (findWay(map, i, j - 1)) {
                    System.out.println(String.format("map[%d][%d]可以走通", i, j-1));
                    return true;
                } else {
                    map[i][j] = 3;
                    System.out.println(String.format("map[%d][%d]走不通", i, j));
                    return false;
                }
            } else {
                System.out.println(String.format("map[%d][%d]无法行走", i, j));
                printMap(map);
                return false;
            }
        }
    }

    public static void printMap(int[][] map) {
        for (int[] i : map) {
            for (int j : i) {
                System.out.printf(j + "\t");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static int[][] createMap() {
        int[][] map = new int[8][7];
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 1; i < 7; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }

        map[3][1] = 1;
        map[3][2] = 1;
        map[3][3] = 1;
        map[2][3] = 1;
//        map[1][3] = 1;

        return map;
    }
}
